#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SIZE 1024
int main(int argc, char *argv[])
{
    if (argc != 3)
        return 1;
    std::string ip = argv[1];
    int port = std::stoi(argv[2]);

    int socketfd = socket(AF_INET, SOCK_DGRAM, 0);
    struct sockaddr_in svraddr;
    socklen_t addrlen = sizeof(svraddr);
    svraddr.sin_family = AF_INET;
    svraddr.sin_port = htons(port);
    inet_aton(ip.c_str(), &svraddr.sin_addr);
    std::string message;
    char buff[SIZE] = {0};
    while (std::getline(std::cin, message))
    {
        int n = sendto(socketfd, message.c_str(), message.size(), 0, (struct sockaddr *)&svraddr, sizeof(svraddr));
        n = recvfrom(socketfd, buff, sizeof(buff), 0, (struct sockaddr *)&svraddr, &addrlen);
        if (n > 0)
        {
            buff[n] = '\0';
            printf("sever say: %s\n", buff);
        }
    }
    close(socketfd);
    return 0;
}